package cn.com.tomato.controller;

import cn.com.tomato.controller.domain.AjaxResult;
import cn.com.tomato.controller.domain.todoSet.TodoSetVo;
import cn.com.tomato.model.Focus;
import cn.com.tomato.service.FocusService;
import cn.hutool.core.date.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @Author: chenlj
 * @CreateTime: 2024-08-29 15:45
 * @Description: todo
 */
@RestController
@RequestMapping("/tomato/focus")
public class FocusController extends BaseController{

    @Autowired
    private FocusService focusService;

    /**
     * 获取一天专注
     * @param focus
     * @return
     */
    @PostMapping("/getByDay")
    public AjaxResult getByDay(@RequestBody Focus focus){
        logger.info("date:{}", focus);
        if( focus.getTid()!= null && focus.getTid() == -1){
            focus.setTid(null);
        }
        focus.setStart(DateUtil.beginOfDay(focus.getStart()));
        focus.setEnd(DateUtil.endOfDay(focus.getStart()));
        logger.info("date:{}", focus);
        List<Focus> focusList = focusService.selectFocusList(focus);
        return success(focusList);
    }

    /**
     * 获取一个月专注
     * @param focus
     * @return
     */
    @PostMapping("/getByMonth")
    public AjaxResult getByMonth(@RequestBody Focus focus){
        focus.setStart(DateUtil.beginOfMonth(focus.getStart()));
        focus.setEnd(DateUtil.endOfMonth(focus.getStart()));
        logger.info("date:{}", focus);
        List<Focus> focusList = focusService.selectFocusList(focus);
        return success(focusList);
    }

    /**
     *  获取一个月内有专注数据的天数
     */
    @PostMapping("/getStartDayByMonth")
    public AjaxResult getStartDayByParam(@RequestBody Focus focus){
        if( focus.getTid()!= null && focus.getTid() == -1){
            focus.setTid(null);
        }
        focus.setStart(DateUtil.beginOfMonth(focus.getStart()));
        focus.setEnd(DateUtil.endOfMonth(focus.getStart()));
        logger.info("date:{}", focus);
        List<Date> dateList = focusService.selectStartDay(focus);
        List<String> collect = dateList.stream().map(date -> {
            return new SimpleDateFormat("yyyy-MM-dd").format(date);
        }).distinct().sorted().collect(Collectors.toList());
        return success(collect);
    }

    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable(name = "id") Long id){
        focusService.deleteFocusById(id);
        return success();
    }

}
